package com.temp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.temp.dto.SearchLangDTO;
import com.temp.entity.Language;
import com.temp.mapper.LanguageMapper;
import com.temp.service.LanguageService;
import com.temp.vo.CustomPageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

@Service
public class LanguageServiceImpl extends ServiceImpl<LanguageMapper, Language> implements LanguageService {

    @Autowired
    private LanguageMapper languageMapper;

    @Override
    public CustomPageVO<Language> findAllLanguage(SearchLangDTO langDto) {
        CustomPageVO<Language> customPageVO = new CustomPageVO<>();
        Integer count = Optional.ofNullable(languageMapper.countAllLanguage(langDto)).orElse(0);
        customPageVO.setTotal(count);
        if(count < 1) {
            customPageVO.setDataList(Collections.emptyList());
            return customPageVO;
        }

        List<Language> languages = languageMapper.findAllLanguage(langDto);
        customPageVO.setDataList(languages);
        return customPageVO;
    }
}
